﻿using System.Collections;
using System.Collections.Generic;
using Script.Net;
using Script.UI;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace Script.UI
{
    public class LoginWnd : UIBase
    {
        public InputField account;
        public InputField passWord;
        public GameObject loginNote;
        public Button exitBtn;
        public Button loginBtn;
        protected override void OnAwake()
        {
            exitBtn.onClick.AddListener(OnClickExitBtn);
        }

        public void OnClickExitBtn()
        {
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
            Application.Quit();
#endif
        }
        
        public void OnCLickLoginBtn()
        {
#if UNITY_EDITOR
            account.text = "bigscr";
            passWord.text = "bigscr123";
#endif
            Debug.Log("[LoginView] OnClick Login");
            loginNote.SetActive(true);
            NetManager.Ins.Login(account.text, passWord.text, ((request, data) =>
            {
                loginNote.SetActive(false);
                if (!data.ContainsKey("error"))
                {
                    SceneManager.LoadScene("main");
                    SceneManager.LoadScene("workshop", LoadSceneMode.Additive);
                }
                else
                {
                    Debug.Log("登陆失败");
                    var wnd = DialogWnd.GetWnd();
                    wnd.SetTitle("提示");
                    wnd.SetContent("登录失败\n用户名或密码无效！");
                    wnd.SetBtnType(ShowBtnType.One);
                    wnd.SetParentWithLayer();
                    loginBtn.gameObject.SetActive(true);
                }
            }));
        }
    }

}
